<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Smoothing Images</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d9/df8/tutorial_root.html">OpenCV Tutorials</a></li><li class="navelem"><a class="el" href="../../d7/da8/tutorial_table_of_content_imgproc.html">Image Processing (imgproc module)</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Smoothing Images </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p><b>Prev Tutorial:</b> <a class="el" href="../../df/d61/tutorial_random_generator_and_text.html">Random generator and text with OpenCV</a></p>
<p><b>Next Tutorial:</b> <a class="el" href="../../db/df6/tutorial_erosion_dilatation.html">Eroding and Dilating</a></p>
<table class="doxtable">
<tr>
<th align="right"></th><th align="left"></th></tr>
<tr>
<td align="right">Original author </td><td align="left">Ana Huamán </td></tr>
<tr>
<td align="right">Compatibility </td><td align="left">OpenCV &gt;= 3.0 </td></tr>
</table>
<h2>Goal </h2>
<p>In this tutorial you will learn how to apply diverse linear filters to smooth images using OpenCV functions such as:</p>
<ul>
<li><b><a class="el" href="../../d4/d86/group__imgproc__filter.html#ga8c45db9afe636703801b0b2e440fce37" title="Blurs an image using the normalized box filter. ">blur()</a></b></li>
<li><b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gaabe8c836e97159a9193fb0b11ac52cf1" title="Blurs an image using a Gaussian filter. ">GaussianBlur()</a></b></li>
<li><b><a class="el" href="../../d4/d86/group__imgproc__filter.html#ga564869aa33e58769b4469101aac458f9" title="Blurs an image using the median filter. ">medianBlur()</a></b></li>
<li><b><a class="el" href="../../d4/d86/group__imgproc__filter.html#ga9d7064d478c95d60003cf839430737ed" title="Applies the bilateral filter to an image. ">bilateralFilter()</a></b></li>
</ul>
<h2>Theory </h2>
<dl class="section note"><dt>Note</dt><dd>The explanation below belongs to the book <a href="http://szeliski.org/Book/">Computer Vision: Algorithms and Applications</a> by Richard Szeliski and to <em>LearningOpenCV</em></dd></dl>
<ul>
<li><em>Smoothing</em>, also called <em>blurring</em>, is a simple and frequently used image processing operation.</li>
<li>There are many reasons for smoothing. In this tutorial we will focus on smoothing in order to reduce noise (other uses will be seen in the following tutorials).</li>
<li><p class="startli">To perform a smoothing operation we will apply a <em>filter</em> to our image. The most common type of filters are <em>linear</em>, in which an output pixel's value (i.e. \(g(i,j)\)) is determined as a weighted sum of input pixel values (i.e. \(f(i+k,j+l)\)) :</p>
<p class="formulaDsp">
\[g(i,j) = \sum_{k,l} f(i+k, j+l) h(k,l)\]
</p>
<p class="startli">\(h(k,l)\) is called the <em>kernel</em>, which is nothing more than the coefficients of the filter.</p>
<p class="startli">It helps to visualize a <em>filter</em> as a window of coefficients sliding across the image.</p>
</li>
<li>There are many kind of filters, here we will mention the most used:</li>
</ul>
<h3>Normalized Box Filter</h3>
<ul>
<li>This filter is the simplest of all! Each output pixel is the <em>mean</em> of its kernel neighbors ( all of them contribute with equal weights)</li>
<li><p class="startli">The kernel is below:</p>
<p class="formulaDsp">
\[K = \dfrac{1}{K_{width} \cdot K_{height}} \begin{bmatrix} 1 &amp; 1 &amp; 1 &amp; ... &amp; 1 \\ 1 &amp; 1 &amp; 1 &amp; ... &amp; 1 \\ . &amp; . &amp; . &amp; ... &amp; 1 \\ . &amp; . &amp; . &amp; ... &amp; 1 \\ 1 &amp; 1 &amp; 1 &amp; ... &amp; 1 \end{bmatrix}\]
</p>
</li>
</ul>
<h3>Gaussian Filter</h3>
<ul>
<li>Probably the most useful filter (although not the fastest). Gaussian filtering is done by convolving each point in the input array with a <em>Gaussian kernel</em> and then summing them all to produce the output array.</li>
<li><p class="startli">Just to make the picture clearer, remember how a 1D Gaussian kernel look like?</p>
<div class="image">
<img src="../../Smoothing_Tutorial_theory_gaussian_0.jpg" alt="Smoothing_Tutorial_theory_gaussian_0.jpg"/>
</div>
<p class="startli">Assuming that an image is 1D, you can notice that the pixel located in the middle would have the biggest weight. The weight of its neighbors decreases as the spatial distance between them and the center pixel increases.</p>
<dl class="section note"><dt>Note</dt><dd>Remember that a 2D Gaussian can be represented as : <p class="formulaDsp">
\[G_{0}(x, y) = A e^{ \dfrac{ -(x - \mu_{x})^{2} }{ 2\sigma^{2}_{x} } + \dfrac{ -(y - \mu_{y})^{2} }{ 2\sigma^{2}_{y} } }\]
</p>
 where \(\mu\) is the mean (the peak) and \(\sigma^{2}\) represents the variance (per each of the variables \(x\) and \(y\))</dd></dl>
<h3>Median Filter</h3>
</li>
</ul>
<p>The median filter run through each element of the signal (in this case the image) and replace each pixel with the <b>median</b> of its neighboring pixels (located in a square neighborhood around the evaluated pixel).</p>
<h3>Bilateral Filter</h3>
<ul>
<li>So far, we have explained some filters which main goal is to <em>smooth</em> an input image. However, sometimes the filters do not only dissolve the noise, but also smooth away the <em>edges</em>. To avoid this (at certain extent at least), we can use a bilateral filter.</li>
<li>In an analogous way as the Gaussian filter, the bilateral filter also considers the neighboring pixels with weights assigned to each of them. These weights have two components, the first of which is the same weighting used by the Gaussian filter. The second component takes into account the difference in intensity between the neighboring pixels and the evaluated one.</li>
<li>For a more detailed explanation you can check <a href="http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html">this link</a></li>
</ul>
<h2>Code </h2>
<ul>
<li><b>What does this program do?</b><ul>
<li>Loads an image</li>
<li>Applies 4 different kinds of filters (explained in Theory) and show the filtered images sequentially</li>
</ul>
</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgProc/Smoothing/Smoothing.cpp">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>std;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> DELAY_CAPTION = 1500;</div><div class="line"><span class="keywordtype">int</span> DELAY_BLUR = 100;</div><div class="line"><span class="keywordtype">int</span> MAX_KERNEL_LENGTH = 31;</div><div class="line"></div><div class="line"><a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> src; <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> dst;</div><div class="line"><span class="keywordtype">char</span> window_name[] = <span class="stringliteral">&quot;Smoothing Demo&quot;</span>;</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> display_caption( <span class="keyword">const</span> <span class="keywordtype">char</span>* caption );</div><div class="line"><span class="keywordtype">int</span> display_dst( <span class="keywordtype">int</span> delay );</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> ** argv )</div><div class="line">{</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>( window_name, <a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a> );</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>* filename = argc &gt;=2 ? argv[1] : <span class="stringliteral">&quot;lena.jpg&quot;</span>;</div><div class="line"></div><div class="line">    src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( <a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>( filename ), <a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a> );</div><div class="line">    <span class="keywordflow">if</span> (src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>())</div><div class="line">    {</div><div class="line">        printf(<span class="stringliteral">&quot; Error opening image\n&quot;</span>);</div><div class="line">        printf(<span class="stringliteral">&quot; Usage:\n %s [image_name-- default lena.jpg] \n&quot;</span>, argv[0]);</div><div class="line">        <span class="keywordflow">return</span> EXIT_FAILURE;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span>( display_caption( <span class="stringliteral">&quot;Original Image&quot;</span> ) != 0 )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    dst = src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#adff2ea98da45eae0833e73582dd4a660">clone</a>();</div><div class="line">    <span class="keywordflow">if</span>( display_dst( DELAY_CAPTION ) != 0 )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span>( display_caption( <span class="stringliteral">&quot;Homogeneous Blur&quot;</span> ) != 0 )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2 )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga8c45db9afe636703801b0b2e440fce37">blur</a>( src, dst, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( i, i ), <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(-1,-1) );</div><div class="line">        <span class="keywordflow">if</span>( display_dst( DELAY_BLUR ) != 0 )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span>( display_caption( <span class="stringliteral">&quot;Gaussian Blur&quot;</span> ) != 0 )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2 )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d4/d86/group__imgproc__filter.html#gaabe8c836e97159a9193fb0b11ac52cf1">GaussianBlur</a>( src, dst, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( i, i ), 0, 0 );</div><div class="line">        <span class="keywordflow">if</span>( display_dst( DELAY_BLUR ) != 0 )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span>( display_caption( <span class="stringliteral">&quot;Median Blur&quot;</span> ) != 0 )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2 )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga564869aa33e58769b4469101aac458f9">medianBlur</a> ( src, dst, i );</div><div class="line">        <span class="keywordflow">if</span>( display_dst( DELAY_BLUR ) != 0 )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span>( display_caption( <span class="stringliteral">&quot;Bilateral Blur&quot;</span> ) != 0 )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2 )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga9d7064d478c95d60003cf839430737ed">bilateralFilter</a> ( src, dst, i, i*2, i/2 );</div><div class="line">        <span class="keywordflow">if</span>( display_dst( DELAY_BLUR ) != 0 )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">    display_caption( <span class="stringliteral">&quot;Done!&quot;</span> );</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> display_caption( <span class="keyword">const</span> <span class="keywordtype">char</span>* caption )</div><div class="line">{</div><div class="line">    dst = Mat::zeros( src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a146f8e8dda07d1365a575ab83d9828d1">size</a>(), src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#af2d2652e552d7de635988f18a84b53e5">type</a>() );</div><div class="line">    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576">putText</a>( dst, caption,</div><div class="line">             <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>/4, src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>/2),</div><div class="line">             <a class="code" href="../../d6/d6e/group__imgproc__draw.html#gga0f9314ea6e35f99bb23f29567fc16e11af7b1b25521fc9b5731a97cfd13460c2a">FONT_HERSHEY_COMPLEX</a>, 1, <a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a>(255, 255, 255) );</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> display_dst(DELAY_CAPTION);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> display_dst( <span class="keywordtype">int</span> delay )</div><div class="line">{</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( window_name, dst );</div><div class="line">    <span class="keywordtype">int</span> c = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a> ( delay );</div><div class="line">    <span class="keywordflow">if</span>( c &gt;= 0 ) { <span class="keywordflow">return</span> -1; }</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgProc/Smoothing/Smoothing.java">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> org.opencv.core.*;</div><div class="line"><span class="keyword">import</span> org.opencv.highgui.HighGui;</div><div class="line"><span class="keyword">import</span> org.opencv.imgcodecs.Imgcodecs;</div><div class="line"><span class="keyword">import</span> org.opencv.imgproc.Imgproc;</div><div class="line"></div><div class="line"><span class="keyword">class </span>SmoothingRun {</div><div class="line"></div><div class="line">    <span class="keywordtype">int</span> DELAY_CAPTION = 1500;</div><div class="line">    <span class="keywordtype">int</span> DELAY_BLUR = 100;</div><div class="line">    <span class="keywordtype">int</span> MAX_KERNEL_LENGTH = 31;</div><div class="line"></div><div class="line">    Mat src = <span class="keyword">new</span> Mat(), dst = <span class="keyword">new</span> Mat();</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> windowName = <span class="stringliteral">&quot;Filter Demo 1&quot;</span>;</div><div class="line"></div><div class="line">    <span class="keyword">public</span> <span class="keywordtype">void</span> run(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line"></div><div class="line">        <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> filename = ((args.length &gt; 0) ? args[0] : <span class="stringliteral">&quot;../data/lena.jpg&quot;</span>);</div><div class="line"></div><div class="line">        src = Imgcodecs.imread(filename, Imgcodecs.IMREAD_COLOR);</div><div class="line">        <span class="keywordflow">if</span>( src.empty() ) {</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Error opening image&quot;</span>);</div><div class="line">            System.out.println(<span class="stringliteral">&quot;Usage: ./Smoothing [image_name -- default ../data/lena.jpg] \n&quot;</span>);</div><div class="line">            System.exit(-1);</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span>( displayCaption( <span class="stringliteral">&quot;Original Image&quot;</span> ) != 0 ) { System.exit(0); }</div><div class="line"></div><div class="line">        dst = src.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#adff2ea98da45eae0833e73582dd4a660">clone</a>();</div><div class="line">        <span class="keywordflow">if</span>( displayDst( DELAY_CAPTION ) != 0 ) { System.exit(0); }</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span>( displayCaption( <span class="stringliteral">&quot;Homogeneous Blur&quot;</span> ) != 0 ) { System.exit(0); }</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2) {</div><div class="line">            Imgproc.blur(src, dst, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(i, i), <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(-1, -1));</div><div class="line">            displayDst(DELAY_BLUR);</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span>( displayCaption( <span class="stringliteral">&quot;Gaussian Blur&quot;</span> ) != 0 ) { System.exit(0); }</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2) {</div><div class="line">            Imgproc.GaussianBlur(src, dst, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(i, i), 0, 0);</div><div class="line">            displayDst(DELAY_BLUR);</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span>( displayCaption( <span class="stringliteral">&quot;Median Blur&quot;</span> ) != 0 ) { System.exit(0); }</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2) {</div><div class="line">            Imgproc.medianBlur(src, dst, i);</div><div class="line">            displayDst(DELAY_BLUR);</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span>( displayCaption( <span class="stringliteral">&quot;Bilateral Blur&quot;</span> ) != 0 ) { System.exit(0); }</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2) {</div><div class="line">            Imgproc.bilateralFilter(src, dst, i, i * 2, i / 2);</div><div class="line">            displayDst(DELAY_BLUR);</div><div class="line">        }</div><div class="line"></div><div class="line">        displayCaption( <span class="stringliteral">&quot;Done!&quot;</span> );</div><div class="line"></div><div class="line">        System.exit(0);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">int</span> displayCaption(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> caption) {</div><div class="line">        dst = Mat.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a0b57b6a326c8876d944d188a46e0f556">zeros</a>(src.size(), src.type());</div><div class="line">        Imgproc.putText(dst, caption,</div><div class="line">                <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(src.cols() / 4, src.rows() / 2),</div><div class="line">                Imgproc.FONT_HERSHEY_COMPLEX, 1, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255, 255, 255));</div><div class="line"></div><div class="line">        <span class="keywordflow">return</span> displayDst(DELAY_CAPTION);</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordtype">int</span> displayDst(<span class="keywordtype">int</span> delay) {</div><div class="line">        HighGui.imshow( windowName, dst );</div><div class="line">        <span class="keywordtype">int</span> c = HighGui.waitKey( delay );</div><div class="line">        <span class="keywordflow">if</span> (c &gt;= 0) { <span class="keywordflow">return</span> -1; }</div><div class="line">        <span class="keywordflow">return</span> 0;</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">public</span> <span class="keyword">class </span>Smoothing {</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keywordtype">void</span> main(<a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a>[] args) {</div><div class="line">        <span class="comment">// Load the native library.</span></div><div class="line">        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);</div><div class="line">        <span class="keyword">new</span> SmoothingRun().run(args);</div><div class="line">    }</div><div class="line">}</div></div><!-- fragment -->  </div> </li>
</ul>
 <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'><ul>
<li><b>Downloadable code</b>: Click <a href="https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/imgProc/Smoothing/smoothing.py">here</a></li>
<li><b>Code at glance:</b> <div class="fragment"><div class="line"><span class="keyword">import</span> sys</div><div class="line"><span class="keyword">import</span> cv2 <span class="keyword">as</span> cv</div><div class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</div><div class="line"></div><div class="line"><span class="comment">#  Global Variables</span></div><div class="line"></div><div class="line">DELAY_CAPTION = 1500</div><div class="line">DELAY_BLUR = 100</div><div class="line">MAX_KERNEL_LENGTH = 31</div><div class="line"></div><div class="line">src = <span class="keywordtype">None</span></div><div class="line">dst = <span class="keywordtype">None</span></div><div class="line">window_name = <span class="stringliteral">&#39;Smoothing Demo&#39;</span></div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">def </span>main(argv):</div><div class="line">    <a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">cv.namedWindow</a>(window_name, cv.WINDOW_AUTOSIZE)</div><div class="line"></div><div class="line">    <span class="comment"># Load the source image</span></div><div class="line">    imageName = argv[0] <span class="keywordflow">if</span> len(argv) &gt; 0 <span class="keywordflow">else</span> <span class="stringliteral">&#39;lena.jpg&#39;</span></div><div class="line"></div><div class="line">    <span class="keyword">global</span> src</div><div class="line">    src = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">cv.imread</a>(<a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">cv.samples.findFile</a>(imageName))</div><div class="line">    <span class="keywordflow">if</span> src <span class="keywordflow">is</span> <span class="keywordtype">None</span>:</div><div class="line">        <span class="keywordflow">print</span> (<span class="stringliteral">&#39;Error opening image&#39;</span>)</div><div class="line">        <span class="keywordflow">print</span> (<span class="stringliteral">&#39;Usage: smoothing.py [image_name -- default ../data/lena.jpg] \n&#39;</span>)</div><div class="line">        <span class="keywordflow">return</span> -1</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> display_caption(<span class="stringliteral">&#39;Original Image&#39;</span>) != 0:</div><div class="line">        <span class="keywordflow">return</span> 0</div><div class="line"></div><div class="line">    <span class="keyword">global</span> dst</div><div class="line">    dst = np.copy(src)</div><div class="line">    <span class="keywordflow">if</span> display_dst(DELAY_CAPTION) != 0:</div><div class="line">        <span class="keywordflow">return</span> 0</div><div class="line"></div><div class="line">    <span class="comment"># Applying Homogeneous blur</span></div><div class="line">    <span class="keywordflow">if</span> display_caption(<span class="stringliteral">&#39;Homogeneous Blur&#39;</span>) != 0:</div><div class="line">        <span class="keywordflow">return</span> 0</div><div class="line"></div><div class="line">    </div><div class="line">    <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(1, MAX_KERNEL_LENGTH, 2):</div><div class="line">        dst = <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga8c45db9afe636703801b0b2e440fce37">cv.blur</a>(src, (i, i))</div><div class="line">        <span class="keywordflow">if</span> display_dst(DELAY_BLUR) != 0:</div><div class="line">            <span class="keywordflow">return</span> 0</div><div class="line">    </div><div class="line"></div><div class="line">    <span class="comment"># Applying Gaussian blur</span></div><div class="line">    <span class="keywordflow">if</span> display_caption(<span class="stringliteral">&#39;Gaussian Blur&#39;</span>) != 0:</div><div class="line">        <span class="keywordflow">return</span> 0</div><div class="line"></div><div class="line">    </div><div class="line">    <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(1, MAX_KERNEL_LENGTH, 2):</div><div class="line">        dst = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gaabe8c836e97159a9193fb0b11ac52cf1">cv.GaussianBlur</a>(src, (i, i), 0)</div><div class="line">        <span class="keywordflow">if</span> display_dst(DELAY_BLUR) != 0:</div><div class="line">            <span class="keywordflow">return</span> 0</div><div class="line">    </div><div class="line"></div><div class="line">    <span class="comment"># Applying Median blur</span></div><div class="line">    <span class="keywordflow">if</span> display_caption(<span class="stringliteral">&#39;Median Blur&#39;</span>) != 0:</div><div class="line">        <span class="keywordflow">return</span> 0</div><div class="line"></div><div class="line">    </div><div class="line">    <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(1, MAX_KERNEL_LENGTH, 2):</div><div class="line">        dst = <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga564869aa33e58769b4469101aac458f9">cv.medianBlur</a>(src, i)</div><div class="line">        <span class="keywordflow">if</span> display_dst(DELAY_BLUR) != 0:</div><div class="line">            <span class="keywordflow">return</span> 0</div><div class="line">    </div><div class="line"></div><div class="line">    <span class="comment"># Applying Bilateral Filter</span></div><div class="line">    <span class="keywordflow">if</span> display_caption(<span class="stringliteral">&#39;Bilateral Blur&#39;</span>) != 0:</div><div class="line">        <span class="keywordflow">return</span> 0</div><div class="line"></div><div class="line">    </div><div class="line">    <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(1, MAX_KERNEL_LENGTH, 2):</div><div class="line">        dst = <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga9d7064d478c95d60003cf839430737ed">cv.bilateralFilter</a>(src, i, i * 2, i / 2)</div><div class="line">        <span class="keywordflow">if</span> display_dst(DELAY_BLUR) != 0:</div><div class="line">            <span class="keywordflow">return</span> 0</div><div class="line">    </div><div class="line"></div><div class="line">    <span class="comment">#  Done</span></div><div class="line">    display_caption(<span class="stringliteral">&#39;Done!&#39;</span>)</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">def </span>display_caption(caption):</div><div class="line">    <span class="keyword">global</span> dst</div><div class="line">    dst = np.zeros(src.shape, src.dtype)</div><div class="line">    rows, cols, _ch = src.shape</div><div class="line">    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576">cv.putText</a>(dst, caption,</div><div class="line">                (int(cols / 4), int(rows / 2)),</div><div class="line">                cv.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255))</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> display_dst(DELAY_CAPTION)</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">def </span>display_dst(delay):</div><div class="line">    <a class="code" href="../../df/d24/group__highgui__opengl.html#gaae7e90aa3415c68dba22a5ff2cefc25d">cv.imshow</a>(window_name, dst)</div><div class="line">    c = <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv.waitKey</a>(delay)</div><div class="line">    <span class="keywordflow">if</span> c &gt;= 0 : <span class="keywordflow">return</span> -1</div><div class="line">    <span class="keywordflow">return</span> 0</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordflow">if</span> __name__ == <span class="stringliteral">&quot;__main__&quot;</span>:</div><div class="line">    main(sys.argv[1:])</div></div><!-- fragment -->  </div> </li>
</ul>
<h2>Explanation </h2>
<p>Let's check the OpenCV functions that involve only the smoothing procedure, since the rest is already known by now.</p>
<h4>Normalized Block Filter:</h4>
<ul>
<li>OpenCV offers the function <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#ga8c45db9afe636703801b0b2e440fce37" title="Blurs an image using the normalized box filter. ">blur()</a></b> to perform smoothing with this filter. We specify 4 arguments (more details, check the Reference):<ul>
<li><em>src</em>: Source image</li>
<li><em>dst</em>: Destination image</li>
<li><em>Size( w, h )</em>: Defines the size of the kernel to be used ( of width <em>w</em> pixels and height <em>h</em> pixels)</li>
<li><em>Point(-1, -1)</em>: Indicates where the anchor point (the pixel evaluated) is located with respect to the neighborhood. If there is a negative value, then the center of the kernel is considered the anchor point.</li>
</ul>
</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2 )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga8c45db9afe636703801b0b2e440fce37">blur</a>( src, dst, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( i, i ), <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(-1,-1) );</div><div class="line">        <span class="keywordflow">if</span>( display_dst( DELAY_BLUR ) != 0 )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2) {</div><div class="line">            Imgproc.blur(src, dst, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(i, i), <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(-1, -1));</div><div class="line">            displayDst(DELAY_BLUR);</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(1, MAX_KERNEL_LENGTH, 2):</div><div class="line">        dst = <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga8c45db9afe636703801b0b2e440fce37">cv.blur</a>(src, (i, i))</div><div class="line">        <span class="keywordflow">if</span> display_dst(DELAY_BLUR) != 0:</div><div class="line">            <span class="keywordflow">return</span> 0</div></div><!-- fragment --> </div> <h4>Gaussian Filter:</h4>
<ul>
<li>It is performed by the function <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#gaabe8c836e97159a9193fb0b11ac52cf1" title="Blurs an image using a Gaussian filter. ">GaussianBlur()</a></b> : Here we use 4 arguments (more details, check the OpenCV reference):<ul>
<li><em>src</em>: Source image</li>
<li><em>dst</em>: Destination image</li>
<li><em>Size(w, h)</em>: The size of the kernel to be used (the neighbors to be considered). \(w\) and \(h\) have to be odd and positive numbers otherwise the size will be calculated using the \(\sigma_{x}\) and \(\sigma_{y}\) arguments.</li>
<li>\(\sigma_{x}\): The standard deviation in x. Writing \(0\) implies that \(\sigma_{x}\) is calculated using kernel size.</li>
<li>\(\sigma_{y}\): The standard deviation in y. Writing \(0\) implies that \(\sigma_{y}\) is calculated using kernel size.</li>
</ul>
</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2 )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d4/d86/group__imgproc__filter.html#gaabe8c836e97159a9193fb0b11ac52cf1">GaussianBlur</a>( src, dst, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( i, i ), 0, 0 );</div><div class="line">        <span class="keywordflow">if</span>( display_dst( DELAY_BLUR ) != 0 )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2) {</div><div class="line">            Imgproc.GaussianBlur(src, dst, <span class="keyword">new</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>(i, i), 0, 0);</div><div class="line">            displayDst(DELAY_BLUR);</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(1, MAX_KERNEL_LENGTH, 2):</div><div class="line">        dst = <a class="code" href="../../d4/d86/group__imgproc__filter.html#gaabe8c836e97159a9193fb0b11ac52cf1">cv.GaussianBlur</a>(src, (i, i), 0)</div><div class="line">        <span class="keywordflow">if</span> display_dst(DELAY_BLUR) != 0:</div><div class="line">            <span class="keywordflow">return</span> 0</div></div><!-- fragment --> </div> <h4>Median Filter:</h4>
<ul>
<li>This filter is provided by the <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#ga564869aa33e58769b4469101aac458f9" title="Blurs an image using the median filter. ">medianBlur()</a></b> function: We use three arguments:<ul>
<li><em>src</em>: Source image</li>
<li><em>dst</em>: Destination image, must be the same type as <em>src</em></li>
<li><em>i</em>: Size of the kernel (only one because we use a square window). Must be odd.</li>
</ul>
</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2 )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga564869aa33e58769b4469101aac458f9">medianBlur</a> ( src, dst, i );</div><div class="line">        <span class="keywordflow">if</span>( display_dst( DELAY_BLUR ) != 0 )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2) {</div><div class="line">            Imgproc.medianBlur(src, dst, i);</div><div class="line">            displayDst(DELAY_BLUR);</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(1, MAX_KERNEL_LENGTH, 2):</div><div class="line">        dst = <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga564869aa33e58769b4469101aac458f9">cv.medianBlur</a>(src, i)</div><div class="line">        <span class="keywordflow">if</span> display_dst(DELAY_BLUR) != 0:</div><div class="line">            <span class="keywordflow">return</span> 0</div></div><!-- fragment --> </div> <h4>Bilateral Filter</h4>
<ul>
<li>Provided by OpenCV function <b><a class="el" href="../../d4/d86/group__imgproc__filter.html#ga9d7064d478c95d60003cf839430737ed" title="Applies the bilateral filter to an image. ">bilateralFilter()</a></b> We use 5 arguments:<ul>
<li><em>src</em>: Source image</li>
<li><em>dst</em>: Destination image</li>
<li><em>d</em>: The diameter of each pixel neighborhood.</li>
<li>\(\sigma_{Color}\): Standard deviation in the color space.</li>
<li>\(\sigma_{Space}\): Standard deviation in the coordinate space (in pixel terms)</li>
</ul>
</li>
</ul>
 <div class='newInnerHTML' title='cpp' style='display: none;'>C++</div><div class='toggleable_div label_cpp' style='display: none;'> <div class="fragment"><div class="line">    <span class="keywordflow">for</span> ( <span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2 )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga9d7064d478c95d60003cf839430737ed">bilateralFilter</a> ( src, dst, i, i*2, i/2 );</div><div class="line">        <span class="keywordflow">if</span>( display_dst( DELAY_BLUR ) != 0 )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">return</span> 0;</div><div class="line">        }</div><div class="line">    }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='java' style='display: none;'>Java</div><div class='toggleable_div label_java' style='display: none;'> <div class="fragment"><div class="line">        <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt; MAX_KERNEL_LENGTH; i = i + 2) {</div><div class="line">            Imgproc.bilateralFilter(src, dst, i, i * 2, i / 2);</div><div class="line">            displayDst(DELAY_BLUR);</div><div class="line">        }</div></div><!-- fragment --> </div>  <div class='newInnerHTML' title='python' style='display: none;'>Python</div><div class='toggleable_div label_python' style='display: none;'> <div class="fragment"><div class="line">    <span class="comment"># Remember, bilateral is a bit slow, so as value go higher, it takes long time</span></div><div class="line">    <span class="keywordflow">for</span> i <span class="keywordflow">in</span> range(1, MAX_KERNEL_LENGTH, 2):</div><div class="line">        dst = <a class="code" href="../../d4/d86/group__imgproc__filter.html#ga9d7064d478c95d60003cf839430737ed">cv.bilateralFilter</a>(src, i, i * 2, i / 2)</div><div class="line">        <span class="keywordflow">if</span> display_dst(DELAY_BLUR) != 0:</div><div class="line">            <span class="keywordflow">return</span> 0</div></div><!-- fragment --> </div> <h2>Results </h2>
<ul>
<li>The code opens an image (in this case <a href="https://raw.githubusercontent.com/opencv/opencv/master/samples/data/lena.jpg">lena.jpg</a>) and display it under the effects of the 4 filters explained.</li>
<li><p class="startli">Here is a snapshot of the image smoothed using <em>medianBlur</em>:</p>
<div class="image">
<img src="../../Smoothing_Tutorial_Result_Median_Filter.jpg" alt="Smoothing_Tutorial_Result_Median_Filter.jpg"/>
</div>
 </li>
</ul>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:35 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
